package com.evan.review.architecture.components;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.evan.review.R;
import com.evan.review.architecture.components.paging.ItemAdapter;
import com.evan.review.architecture.components.paging.PagingViewModel;

/**
 * Paging示例Activity
 * 展示Paging库的基本用法
 */
public class PagingExampleActivity extends AppCompatActivity {
    
    private RecyclerView recyclerView;
    private ItemAdapter adapter;
    private PagingViewModel viewModel;
    private ProgressBar progressBar;
    private TextView tvEmpty;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paging_example);
        
        // 设置标题
        setTitle("Paging示例");
        
        // 初始化视图
        initViews();
        
        // 初始化ViewModel
        viewModel = new ViewModelProvider(this).get(PagingViewModel.class);
        
        // 观察数据变化
        observeViewModel();
    }
    
    private void initViews() {
        recyclerView = findViewById(R.id.recycler_view);
        progressBar = findViewById(R.id.progress_bar);
        tvEmpty = findViewById(R.id.tv_empty);
        
        // 设置RecyclerView
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        
        // 添加分割线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
                recyclerView.getContext(), layoutManager.getOrientation());
        recyclerView.addItemDecoration(dividerItemDecoration);
        
        // 创建适配器
        adapter = new ItemAdapter();
        recyclerView.setAdapter(adapter);
        
        // 添加滚动监听器，实现简单的分页加载
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                
                // 检查是否滚动到底部
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int visibleItemCount = layoutManager.getChildCount();
                int totalItemCount = layoutManager.getItemCount();
                int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                
                // 如果距离底部还有5个项目，就加载更多
                if ((visibleItemCount + firstVisibleItemPosition + 5) >= totalItemCount
                        && firstVisibleItemPosition >= 0) {
                    viewModel.loadMoreItems();
                }
            }
        });
    }
    
    private void observeViewModel() {
        // 观察加载状态
        viewModel.getIsLoading().observe(this, isLoading -> {
            progressBar.setVisibility(isLoading ? View.VISIBLE : View.GONE);
        });
        
        // 观察数据列表
        viewModel.getItems().observe(this, items -> {
            if (items.isEmpty()) {
                tvEmpty.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            } else {
                tvEmpty.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
                adapter.submitList(items);
            }
        });
        
        // 加载初始数据
        viewModel.loadInitialItems();
    }
} 